// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'babel-polyfill'
import configs from './config'
import locale from 'element-ui/lib/locale/lang/zh-CN'
import 'element-ui/lib/theme-chalk/index.css';
import './assets/iconfont/iconfont.css'
import axios from 'axios'
import qs from 'qs'
import cookie from 'vue-cookie'
import echarts from 'echarts'


// 允许携带cookie

// axios.defaults.withCredentials=true
// axios.defaults.crossDomain = true;
// axios.defaults.withCredentials=true
Vue.use(ElementUI, {locale})
Vue.config.productionTip = false
Vue.prototype.URL =  configs
Vue.prototype.UserInfo = configs
Vue.prototype.$http = axios;
Vue.prototype.qs = qs;
Vue.prototype.$cookie = cookie;
Vue.prototype.$echarts = echarts


axios.interceptors.request.use(
  config => {
    if (localStorage.JWT_TOKEN) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.Authorization = `${localStorage.JWT_TOKEN}`;

    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });
router.beforeEach((to, from, next) => {
  // 获取 JWT Token
  var cookieuser = cookie.get("userToken");


  if (localStorage.getItem("JWT_TOKEN") == null) {
    //判断是否是在线状态
    // 如果用户在login页面
    if (to.path !== '/login') {

      ElementUI.Message.error("您还没有登录");
      next('/login');
    } else {
      next();
    }
  } else {
    if (to.path !== '/login')
      if (cookieuser == null || cookieuser ==="undefined"||cookieuser === "null") {
        //  cookie 为空 关闭过浏览器
        //  判断token是否过期 已过期重新登录，没过期重新设置cookie
        axios.post(configs.api + 'user/isexpired',qs.stringify({
          token: localStorage.getItem("JWT_TOKEN"),
          usertoken:cookieuser
        }))  .then(function (response) {
            console.log(response)
            if (response.data.flag) {
              if (response.data.data != null) {
                cookie.set("userToken", response.data.data);
              }
              next();
            } else {
              localStorage.removeItem("JWT_TOKEN");
              ElementUI.Message.error("身份已过期请重新登录");
              next('/login');
            }
          }).catch(function (response) {
          console.log(response)
          localStorage.removeItem("JWT_TOKEN");
          ElementUI.Message.error("身份已过期请重新登录");
          next('/login');
        });

        //  token 过期重新登录
      }
      else {
        //  cookie 不为空 没有关闭过浏览器
        //  判断token是否过期 没有过期 放行
        axios.post(configs.api + 'user/isexpired',qs.stringify({
          token: localStorage.getItem("JWT_TOKEN"),
          usertoken:cookieuser
        })).then(function (response) {
            if (response.data.flag) {
              if (response.data.data != null) {
                localStorage.setItem("JWT_TOKEN", response.data.data);
              }
              next();
            } else {
              ElementUI.Message.error(response.data.message);
              localStorage.removeItem("JWT_TOKEN");
              next('/login');
            }
          }).catch(function (response) {
          ElementUI.Message.error(response.data.message);
          localStorage.removeItem("JWT_TOKEN");
          next('/login');

        });
        //  token 过期  重新获取新的token
      }

    next();
  }
});

/* eslint-disable no-new */

new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>'
})
